#include <iostream>
#include <vector>
#include <set>

using std::cout;
using std::endl;
using std::multiset;
using std::vector;

vector<int> twoSum(const vector<int> &nums, int target)
{
    vector<int> vec;
    multiset<int> mset;
    for (int i = 0; i < nums.size(); ++i)
    {
        mset.insert(nums[i]);
    }

    int first;
    for (auto &it : mset)
    {
        if (it * 2 == target)
        {
            int count = mset.count(it);
            if (count == 2)
            {
                first = it;
                break;
            }
        }
        else
        {
            int count = mset.count(target - it);
            if (count != 0)
            {
                first = it;
                break;
            }
        }
    };

    int second = target - first;
    for(int i = 0;i < nums.size(); ++ i)
    {
        if(vec.size() == 2) break;

        if(nums[i] == first)
        {
            vec.push_back(i);
            continue;
        }

        if(nums[i] == second)
        {
            vec.push_back(i);
            continue;
        }
    }
    return vec;
};

int main(int argc, char const *argv[])
{

    vector<int> vec = twoSum({-1,-2,-3,-4,-5},-8);
    for(auto &it : vec)
    {
        cout << it << " ";
    }
    cout << endl;
    getchar();
    return 0;
}
